package chapter_22.io.other;

import java.io.*;

/**
 * 利用输入、输出缓冲流复制文件
 */
public class FileCopyWithBuffer {

    public static void main(String[] args) {

        new File("./src/chapter_22/io/other/copy_build.txt").delete();// 每次运行前删除复制文件

        // 开始时间
        long startTime = System.nanoTime();

        try (FileReader fileReader = new FileReader("./src/chapter_22/io/other/new_build.txt");
             BufferedReader bufferedReader = new BufferedReader(fileReader);
             FileWriter fileWriter = new FileWriter("./src/chapter_22/io/other/copy_build.txt");
             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)
        ) {

            // 首先读取一行文本
            String line = bufferedReader.readLine();

            while (line != null) {
                // 打印复制的字符串
                System.out.println(line);
                // 开始写入数据
                bufferedWriter.write(line);
                // 写一个换行符
                bufferedWriter.newLine();
                // 再读取一行文本
                line = bufferedReader.readLine();
            }

            System.out.println("useTime = " + (System.nanoTime() - startTime) + "纳秒" );

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
